package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.IView.IPublicationOrderDetailView;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;

import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PublicationOrderDetailBean;
import com.cloudsigns.factorymanager2.engineering.customview.DateViewDialog;
import com.cloudsigns.factorymanager2.engineering.presenter.PublicationOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


public class PublicationOrderDetailActivity1 extends Activity implements View.OnClickListener, IPublicationOrderDetailView {
    private View rl_back;
    private TextView tv_title;
    private ScrollView scrollview;
    private EditText edt_publication_content, edt_completion_date;
    private ImageView img_completion_date,img_publication;
    private RelativeLayout rl_area_name;
    private List<String> serverUrlList = new ArrayList<>();
    private PublicationOrderDetailBean publicationOrderBean;
    Map<String, List<MediaBean>> mediaBeanMap = new HashMap<>();
    private LayoutInflater layoutInflater;
    private String TAG = PublicationOrderDetailActivity.class.getSimpleName();
    private String from;
    private OrderParentBean orderParentBean;
    private PublicationOrderDetailPresenter publicationOrderDetailPresenter;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_publication_order_detail1);

        publicationOrderDetailPresenter = new PublicationOrderDetailPresenter(this);
        layoutInflater = getLayoutInflater();
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            from = mIntent.getStringExtra(ConstantUtil.KEY_1);
            if (null != from && !TextUtils.isEmpty(from)) {
                if (from.equals("SendOrderForSuperActivity")) {
                    publicationOrderBean = (PublicationOrderDetailBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
                    initData();
                } else {
                    orderParentBean = (OrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

                    if (null != orderParentBean) {
                        edt_publication_content.setText(orderParentBean.getEngineeringName());
                    }

                    publicationOrderDetailPresenter.getPublicationOrderDetail();
                }
            }

        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        scrollview = findViewById(R.id.scrollview);
        edt_publication_content = findViewById(R.id.edt_publication_content);
        edt_completion_date = findViewById(R.id.edt_completion_date);
        img_completion_date=findViewById(R.id.img_completion_date);
        img_publication=findViewById(R.id.img_publication);
        rl_area_name = findViewById(R.id.rl_area_name);


        tv_title.setText(getResources().getText(R.string.work_order_information));
        rl_back.setOnClickListener(this);
        img_completion_date.setOnClickListener(this);
        img_publication.setOnClickListener(this);

    }

    private void initData() {

        if (null != publicationOrderBean) {
            edt_publication_content.setText(publicationOrderBean.getPublicationContent());
            edt_completion_date.setText(DateUtil.getYyyy_Mm_Dd(publicationOrderBean.getCompleteDate()));

            List<String> picList = publicationOrderBean.getPicList();
            if (null != picList) {
                serverUrlList.clear();
                serverUrlList.addAll(picList);
            }

            List<MediaBean> mediaBeanList = publicationOrderBean.getMediaList();
            if (null != mediaBeanList) {
                for (MediaBean mediaBean : mediaBeanList) {
                    String areaName = mediaBean.getAreaName();
                    if (mediaBeanMap.containsKey(areaName)) {
                        mediaBeanMap.get(areaName).add(mediaBean);
                    } else {
                        List<MediaBean> tmpMediaBeanList = new ArrayList<>();
                        tmpMediaBeanList.add(mediaBean);
                        mediaBeanMap.put(areaName, tmpMediaBeanList);
                    }
                }
            }
        }
    }

    // 完成日期
    private void showDateDialog() {
        DateViewDialog endDateChooseDialog = new DateViewDialog(this,
                new DateViewDialog.DateChooseInterface() {
                    @Override
                    public void getDateTime(String time, boolean longTimeChecked) {
                        edt_completion_date.setText(time);
                    }
                });
        endDateChooseDialog.setTimePickerGone(true);

        Calendar dateAndTime = Calendar.getInstance(Locale.getDefault());

        endDateChooseDialog.setDateDialogTitle(dateAndTime.get(Calendar.YEAR) + "-" + (dateAndTime.get(Calendar.MONTH) + 1) + "-" + dateAndTime.get(Calendar.DAY_OF_MONTH));
        endDateChooseDialog.showDateChooseDialog();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 完成日期
            case R.id.img_completion_date:
                showDateDialog();
                break;
           // 编辑上刊信息
            case R.id.img_publication:
                startActivity(new Intent(PublicationOrderDetailActivity1.this,EditPublicationActivity.class));
                break;
        }
    }

    @Override
    public String getOrderId() {
        if (null != orderParentBean)
            return orderParentBean.getId();
        return null;
    }

    @Override
    public void getPublicationOrderDetailSuccessed(PublicationOrderDetailBean publicationOrderBean) {
        this.publicationOrderBean = publicationOrderBean;
        initData();
    }

    @Override
    public void gePublicationOrderDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
